import React from 'react'
// =====================================================================
import Encrypt from '@com/plugins/encrypt'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== $Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const $time 	= $Lazy.file(()=>import('@com/utils/time'))
const Tips 		= $Lazy.load(()=>import('#tp/tips'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		specStatus: [{ name:'待分发', value:'0' },{ name:'不可分发', value:'-1' }],
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '条码号',		name: 'spec_code' },
			{ label: '标本性状',		name: 'spec_traits',				type:'select',		loading:true,	idStr:'code' },
			{ label: '科室',		name: 'team_id',					type:'select',		loading:true,	idStr:'uuid', onValue:data=>$fn.onKeShi.call(this,data,{}) },
			{ label: '岗位查询',		name: 'project_id',					type:'select',		loading:true,  },
			{ label: '接收时间',		names: ['start_date','end_date'], 	type:'date-range', 	loading:true,	name:'jz-time' },
			{ label: '医院',		name: 'hosp_id',					type:'select',		loading:true },
			{ label: '项目名称',		name: 'kind_id', type: 'select' },
		],
		formData : [
			{ label: '用户名',		name: 'account',	full:true,	required:true },
			{ label: '密码',		name: 'password',	full:true,	type:'password',	required:true },
			{ label: '作废原因',		name: 'reason',		full:true,	type:'textarea',	required:true },
		],
	}
	submit = {}
	query = {team_id:'',}
	
	componentDidMount(){
		this.getSelect()
		$time.then(f=>{
			const arr = f.duration['2']()
			this.resetModel = { start_date: arr[0], end_date: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			if(window.isInit){
				this.fetch()
			}
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 标本性状
			$fn.setRow( forms, 'spec_traits', await f.getDicCache({ code:21000 }) )
			// 科室
			$fn.setRow( forms, 'team_id', await f.getCache({ api:'ke_shi' }) )
			// 岗位
			this.resetData = await f.getCache({ api:'gang_wei' })
			$fn.setRow( forms, 'project_id', this.resetData )
			// 医院
			$fn.setRow( forms, 'hosp_id', await f.getCache({ api:'hospital' }) )
			// 项目名称
			$fn.setRow( forms, 'kind_id', await f.getCache({ api:'labkindselect' } ) )
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'specimen/index',{isIndex:true})
		
	// 表单
	cols = [
		{ title: '条码号', 		dataIndex: 'spec_code', 		...$fn.table.spec_code },
		{ title: '标本性状', 	dataIndex: 'spec_traits_name', 	width:100 },
		{ title: '科室', 		dataIndex: 'team_name', 		width:100 },
		{ title: '岗位组', 		dataIndex: 'project_name', 		width:100 },
		{ title: '项目名称', 	dataIndex: 'kind_name', 		width:180 },
		{ title: '状态', 		dataIndex: 'is_split',  		width:100,	render: text => $fn.getState(this.state.specStatus,text)},
		{ title: '第三方条码',	dataIndex: 'third_spec_code', 	...$fn.table.spec_num },
		{ title: '接收时间', 	dataIndex: 'receive_time',  	...$fn.table.time},
		{ title: '医院', 		dataIndex: 'hosp_name', 		width:180 },
	]
	
	// 操作按钮
	controls = () => {
		return [
			Controls.click.call(this, '条码作废',  (keys,rows) => {
				const spec_code = []
				rows.map(item => spec_code.push(item.spec_code))
				this.submit.spec_code = spec_code
				/* 作废条码时,账号密码自动填充 2021-8-11 --马婷 */
				const userData = $fn.getUser()
				const { empl_name } = userData
				const m = $fn.longSave('remember')
				if($fn.hasObject(m)){
					for(var i in m){ m[i] = Encrypt.decode(m[i]) }
				}
				this.formRef.setValue({account: empl_name, password: m?m.password:'' })
				this.modal.open({formRef: this.formRef})
				
			}, { isKeys:1, code:'F1'} ),
			/* 待分发导出按钮修改 2021-7-24 --马婷 */
			// Controls.disviwExport.call(this, 'specimen/disviwexport', { isKeys:true,query:(keys,rows)=>{
			// 	return {ids: rows.map(v=>v.uuid)}
			// }} ),
			Controls.exports.call(this,'specimen/index',{label:'导出', code:'', 
				param:()=>{
					return {ids: this.state.selectedRows.map(v=>v.uuid)}
				}
			} ),	// 导出
		]
	}
		
	render(){
		return (
			<>
				<Page title='待分发'  controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, {timeStr:'2', resetField:'project_id'}) }
					{/* 表格 */}
					{ Table.call(this, {isIndex:true, isInit: window.isInit} ) }
				</Page>
				{FormModal.call(this,{title:'条码作废', labelWidth:'s6', width:600, api:'specimen/invalid',
					prefix: (<Tips text='请输入用户名与密码进行作废确认' className = 'mb5' />),
					onClear: $fn.clearKeys.bind(this)
				})}
			</>
		)
	}
}

export default Index